import { observable, action } from 'mobx';
import { ListViewPage, RouteLayout, ShareAuth } from 'weaver-mobile-page';
import * as Apis from "../../api/customerApis";
import { Toast, Modal, Tools, Bubble } from 'weaver-mobile';
import * as adapter from '../../constants/adapter';
const alert = Modal.alert;
const getLabel = Tools.getLabel;
export default class CustomerShareListStore {
  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  comObj = {};
  params = {};
  @observable listTab = [];
  @observable listTabKey = "";
  @observable listTabIndex = "my";
  @observable showCheck = false;
  @observable list = {};
  @observable canEdit = false;
  @observable createShareLayout = {};
  @observable initResponsed = false;// 是否已经得到初始化数据的响应
  @observable initSuccessed = false;// 初始化数据是否获取成功
  @observable initFailedMsg = "";   // 初始化处理失败的原因
  @observable shareAuthRoute = {};
  @observable shareConditions = [];
  @observable shareParams = {};

  @action
  doNew = (comObj, params) => {
    this.comObj = comObj;
    this.params = params;
  }

  @action
  doInit = () => {
    let params = this.params;
    Apis.getHearderTab(params).then(data => {
      Apis.shareConditions().then(
        data => {
          this.shareConditions = data.datas[0];
          this.shareConditions1 = data.datas[1];
        }
      )
      this.doInitCallBack(params, data);
    });
  }

  @action
  doInitCallBack = (params, data) => {
    this.initResponsed = true;
    this.initSuccessed = true;
    const currentPath = this.comObj.props.location.pathname;
    this.list = new ListViewPage.Store({ route: currentPath, id: 'customerShareList' });
    this.createShareLayout = new RouteLayout.Store({ route: currentPath, id: 'createShareLayout' });
    this.shareAuthRoute = new ShareAuth.Store({ route: currentPath, id: 'list' });
    this.listTab = data.tabs;
    this.listTabKey = data.keyParam;
    this.canEdit = data.canEdit;
    this.doSearch();
  }

  @action
  doSearch = () => {
    const params = { id: this.params.id };
    Apis.getShareListResult(params).then(data => {
      const dataKey = data.datas;
      this.list.clearData();
      this.list.getData({ dataKey });
    });
  }

  @action
  topTabChange = (selectedIndex) => {
    this.listTabIndex = selectedIndex;
    this.doSearch();
  }

  @action
  doClear = () => {
    this.initResponsed = false;
    this.initSuccessed = false;
    this.initFailedMsg = "";
    this.listTabIndex = "my";
    this.list.clearData();
  }

  @action
  deleteShare = (data) => {
    if (data.isdefault == 1) {
      Toast.fail(getLabel('390952', "不能删除默认共享权限"), 2);
      return;
    }
    alert('', getLabel('83600', '您确认要删除吗？'), [
      { text: getLabel('201', '取消'), onPress: () => { } },
      {
        text: getLabel('83446', '确定'), onPress: () => {
          const params = { id: data.id };
          Toast.loading(getLabel('388626', '正在保存，请稍后...'), 0);
          Apis.deleteShare(params).then(data => {
            Toast.hide();
            this.doSearch();
          });
        }
      },
    ]);
  }
  doSave = (data) => {
    if(this.shareAuthRoute.optionsForm.doValidate().pass){
      let jobtitledepartment = "";
      let jobtitlesubcompany = "";
      //如果类型为岗位级别，将数组中数据取出来
      if (data.sharetype === "6") {
        if (data.jobtitlelevel) {
          if (data.jobtitlelevel.jobtitledepartment) {
            data.jobtitlelevel.jobtitledepartment.forEach(element => {
              if (jobtitledepartment === "") {
                jobtitledepartment = element.id;
              } else {
                jobtitledepartment = jobtitledepartment + "," + element.id;
              }
            })
          }
          if (data.jobtitlelevel.jobtitlesubcompany) {
            data.jobtitlelevel.jobtitlesubcompany.forEach(element => {
              if (jobtitlesubcompany === "") {
                jobtitlesubcompany = element.id;
              } else {
                jobtitlesubcompany = jobtitlesubcompany + "," + element.id;
              }
            })
          }
        }
        this.shareParams = {
          customerId: this.params.id,
          jobtitlelevel: data.jobtitlelevel.jobtitlelevel,
          relatedshareid: data.relatedshareid,
          sharelevel: data.sharelevel,
          sharetype: data.sharetype,
          jobtitlesubcompany: jobtitlesubcompany,
          jobtitledepartment: jobtitledepartment
        }
      } else {
        this.shareParams = {
          customerId: this.params.id,
          sharetype: data.sharetype,
          ...data
        };
      }
      Apis.addShare(this.shareParams).then(data => {
        if (data.status === "success") {
          Bubble.success(getLabel('83551', "保存成功"), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
            if (window.crmGlobal) {
              window.crmGlobal.doSearch();
            }
            this.doInit();
          });
        } else {
          Bubble.error(getLabel(22620, '保存失败'), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
            if (window.crmGlobal) {
              window.crmGlobal.doSearch();
            }
            this.doInit();
          });
        }
      });
    }
    
  }
}